package bmatch.tech;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * Hello world!
 */
public class App {

  public static void main(String[] args) throws Exception {

    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    File configFile = new File("generatorConfig.xml");

    File file = new File("code");
    FileUtils.deleteDirectory(file);

    file.mkdir();
    Properties properties = getProperties();

    ConfigurationParser cp = new ConfigurationParser(properties, warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
    if (!warnings.isEmpty()) {
      for (String warn : warnings) {
        System.out.println(warn);
      }
    }
    System.out.println("生成成功！");
  }

  /**
   * 自定义配置
   */
  private static Properties getProperties() {
    Properties properties = new Properties();
    properties
        .setProperty("url", "jdbc:mysql://rm-bp1s8wez9j4hw42oemo.mysql.rds.aliyuncs.com:3306/exam");
    properties.setProperty("userId", "");
    properties.setProperty("password", "");
    // 包名
    properties.setProperty("package", "");
    // 要生成的表名
    properties.setProperty("tableName", "");
    return properties;
  }
}
